package com.proc;

import com.awaken.*;
import java.util.Calendar;

/**
 * Created by IntelliJ IDEA.
 * User: ndemeshchenko
 * Date: Apr 7, 2010
 * Time: 11:44:46 AM
 */
public class AlarmProcessor implements Runnable{

    AlarmList alarmList;
    Missed missed;
    
    public AlarmProcessor(){}
    public AlarmProcessor(AlarmList alarmList, Missed missed){
        this.alarmList = alarmList;
        this.missed = missed;
    }

    public void run() {
        Logger.log(Thread.currentThread().getName());
        while (true){
            checkTime();
            try{
                Thread.sleep(1000);
            }
            catch (InterruptedException exc){
                Logger.log("TaskManager" + exc.getMessage());
            }
        }
    }

    void checkTime(){
        try{
            for(Object alarm : alarmList.getAlarms()){
                if(     match(((AlarmOne)alarm).getDate()) &&
                        !((AlarmOne)alarm).getIsAlarmed() &&
                        ((AlarmOne)alarm).getStatus().equals("enable")
                  ){
                    ((AlarmOne)alarm).setIsAlarmed(true);
                    //add new notification
                    missed.add( ((AlarmOne)alarm).getTitle());
                }
            }
        }
        catch (Exception exc){
            Logger.log(Thread.currentThread().getName());
            exc.printStackTrace();
        }

    }

    boolean match(Calendar alarmDate){
        Calendar now = Calendar.getInstance();
        return now.get(1) == alarmDate.get(1) &&
                now.get(2) == alarmDate.get(2) &&
                now.get(5) == alarmDate.get(5) &&
                now.get(11) == alarmDate.get(11) &&
                now.get(12) == alarmDate.get(12);
    }
}
